package game;

import game.components.Camera;

import game.players.SoccerPlayer;
import game.players.cpu.opponent.CornerKickerThrowInerCPUOpp;
import game.players.user.CornerKickerThrowInerUser;
import game.scene.LeftCornerKickScene;
import game.scene.LeftGoalKickScene;
import game.scene.MatchScene;
import game.scene.PlayerServeScene;
import game.scene.RightCornerKickScene;
import game.scene.RightGoalKickScene;

import java.awt.Dimension;
import java.awt.Graphics2D;

import com.uqbar.vainilla.DesktopGameLauncher;
import com.uqbar.vainilla.Game;

public class Goal3 extends Game {
	
	/**
	 * Estas variables estáticas dependen de la PC donde se arranque el juego para que funcionen
	 * como se espera asi que las incluimos acá para que sean más fáciles de modificar.
	 */
	public static final int PASS_SPEED          = 10;
	public static final int SHOOT_SPEED         = 25;
	public static final double PLAYER_SPEED     = 3;
	public static final double MAX_PLAYER_SPEED = 5;
	
	private MatchScene scene;

	@Override
	protected void initializeResources() {
		this.setScene(new PlayerServeScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()), 0, 0, 0, 0));
	}

	@Override
	protected void setUpScenes() {
		this.setCurrentScene(this.getScene());
	}

	@Override
	public Dimension getDisplaySize() {
		return new Dimension(800,600);
	}

	@Override
	public String getTitle() {
		return "GOAL 3";
	}
	
	@Override
	public void takeStep(Graphics2D graphics) {
		if (this.getScene().isTimeOver()) {
			this.getScene().showFinalScene();			
		}
		super.takeStep(graphics);
	}
	
	//SCENE CHANGES
	public void changeScene() {
		this.setScene(new PlayerServeScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()),
				this.getScene().getPlayerScore().getScore(), this.getScene().getEnemyScore().getScore(),
				this.getScene().getTimer().getMinutes(),
				this.getScene().getTimer().getSeconds()));
		this.setCurrentScene(this.getScene());
	}
	
	public void changeScene(MatchScene scene) {
		this.setScene(scene);
		this.setCurrentScene(this.getScene());
	}
	
	public void changeToPostGoalScene(MatchScene scene) {		
		this.changeScene(scene);
	}
	
	public void changeToRightGoalKickScene() {
		this.changeScene(new RightGoalKickScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()),
				this.getScene().getPlayerScore().getScore(), this.getScene().getEnemyScore().getScore(),
				this.getScene().getTimer().getMinutes(),
				this.getScene().getTimer().getSeconds()));
		this.getScene().getSoccerBall().locateBall(1010,-100);
		this.getScene().getOppGoalkeeper().move(-20,-50);
	}
	
	public void changeToLeftGoalKickScene() {
		this.changeScene(new LeftGoalKickScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()),
				this.getScene().getPlayerScore().getScore(), this.getScene().getEnemyScore().getScore(),
				this.getScene().getTimer().getMinutes(),
				this.getScene().getTimer().getSeconds()));
		this.getScene().getSoccerBall().locateBall(-980,-80);
		this.getScene().getMateGoalkeeper().move(20,-30);		
	}
	
	public void changeToRightNorthCornerKickScene() {		
		this.changeScene(new RightCornerKickScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()),
				this.getScene().getPlayerScore().getScore(), this.getScene().getEnemyScore().getScore(),
				this.getScene().getTimer().getMinutes(),
				this.getScene().getTimer().getSeconds()));
		this.getScene().getSoccerBall().locateBall(1000,-210);
		this.getScene().getSoccerBall().move(120,-210);
		this.getScene().getUserPlayer().setState(new CornerKickerThrowInerUser(this.getScene().getUserPlayer()));
		this.getScene().getUserPlayer().move(1100,-400);
	}
	
	public void changeToRightSouthCornerKickScene() {
		this.changeScene(new RightCornerKickScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()),
				this.getScene().getPlayerScore().getScore(), this.getScene().getEnemyScore().getScore(),
				this.getScene().getTimer().getMinutes(),
				this.getScene().getTimer().getSeconds()));
		this.getScene().getSoccerBall().locateBall(1150,310);
		this.getScene().getSoccerBall().move(150,175);
		this.getScene().getUserPlayer().setState(new CornerKickerThrowInerUser(this.getScene().getUserPlayer()));
		this.getScene().getUserPlayer().move(1270,530);
	}
	
	public void changeToLeftNorthCornerKickScene() {
		this.changeScene(new LeftCornerKickScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()),
				this.getScene().getPlayerScore().getScore(), this.getScene().getEnemyScore().getScore(),
				this.getScene().getTimer().getMinutes(),
				this.getScene().getTimer().getSeconds()));
		this.getScene().getSoccerBall().locateBall(-950,-200);
		this.getScene().getSoccerBall().move(-140,-210);
		this.getScene().getOpponents().get(0).setState(new CornerKickerThrowInerCPUOpp(this.getScene().getOpponents().get(0)));
		this.getScene().getOpponents().get(0).move(-1625,-260);	
	}
	
	public void changeToLeftSouthCornerKickScene() {
		this.changeScene(new LeftCornerKickScene(new Camera(this.getDisplayWidth(), this.getDisplayHeight()),
				this.getScene().getPlayerScore().getScore(), this.getScene().getEnemyScore().getScore(),
				this.getScene().getTimer().getMinutes(),
				this.getScene().getTimer().getSeconds()));
		this.getScene().getSoccerBall().locateBall(-1010,360);
		this.getScene().getSoccerBall().move(-280,125);
		this.getScene().getOpponents().get(0).setState(new CornerKickerThrowInerCPUOpp(this.getScene().getOpponents().get(0)));
		this.getScene().getOpponents().get(0).move(-1825,675);			
	}
	
	public void changeToOppNorthThrowInScene(double x, double y) {
		this.prepareSceneToNorthThrowIn(x, y);
		//ubico a un jugador para hacer el lateral
		SoccerPlayer throwIner = this.scene.getOppCloserBall();
		throwIner.setState(new CornerKickerThrowInerCPUOpp(throwIner));
		this.locateThrowIner(throwIner);
	}
	
	public void changeToUserNorthThrowInScene(double x, double y) {
		this.prepareSceneToNorthThrowIn(x, y);
		//ubico a un jugador para hacer el lateral
		SoccerPlayer throwIner = this.scene.getUserPlayer();
		throwIner.setState(new CornerKickerThrowInerUser(throwIner));
		this.locateThrowIner(throwIner);						
	}
	
	public void prepareSceneToNorthThrowIn(double x,double y){
		//corro al jugador que llevaba la pelota
		this.scene.getSoccerBall().getLastTouched().move(0,500);
		this.prepareSceneToThrowIn(x, y);	
	}
	
	public void locateThrowIner(SoccerPlayer throwIner){
		throwIner.setX(this.scene.getSoccerBall().getX());
		throwIner.setY((this.scene.getSoccerBall().getY()-throwIner.getAppearance().getHeight())+20);
	}
	
	public void changeToOppSouthThrowInScene(double x, double y) {
		this.prepareSceneToSouthThrowIn(x, y);
		//ubico a un jugador para hacer el lateral
		SoccerPlayer throwIner = this.scene.getOppCloserBall();
		throwIner.setState(new CornerKickerThrowInerCPUOpp(throwIner));
		this.locateThrowIner(throwIner);		
	}

	public void changeToUserSouthThrowInScene(double x, double y) {
		this.prepareSceneToSouthThrowIn(x, y);
		//ubico a un jugador para hacer el lateral
		SoccerPlayer throwIner = this.scene.getUserPlayer();
		throwIner.setState(new CornerKickerThrowInerUser(throwIner));
		this.locateThrowIner(throwIner);		
	}
	
	public void prepareSceneToSouthThrowIn(double x,double y){
		//corro al jugador que llevaba la pelota
		this.scene.getSoccerBall().getLastTouched().move(0,-500);
		this.prepareSceneToThrowIn(x, y);	
	}
	
	public void prepareSceneToThrowIn(double x,double y){
		//acomodo la pelota para hacer el lateral
		this.scene.getSoccerBall().setX(x);
		this.scene.getSoccerBall().setY(y);
		this.scene.setAnnouncing(false);
		this.scene.destroyAnnunciator();
	}
	
	public static void main(String[] args) {
		new DesktopGameLauncher(new Goal3()).launch();
	}
	
	public MatchScene getScene() {
		return scene;
	}

	public void setScene(MatchScene scene) {
		this.scene = scene;
	}

}
